/**
 * 
 */
package com.testing.json;

import com.google.gson.Gson;

/**
 * Similar to {@link JSONToXMLHelper JSONToXMLHelper}; however this utility
 * is using GSON instead of net.sf.JSon 
 *
 * @author Jason
 * created at: Nov 4, 2011
 */
public class GSonHelper {
	
	/**
	 * method to transform a given Object to a GSON; simply passing 
	 * all the raw values to the GSON; might need to cater for Collection
	 * types later on.
	 * 
	 * @param pObject
	 * @return
	 */
	public String transformObjectToJSONString(final Object pObject) {
		Gson aGObj=new Gson();
		String aGString=null;
		
		if (pObject!=null) {
			aGString=aGObj.toJson(pObject);
		}
		return aGString;
	}
	
	/**
	 * method to transform a given JSON String back to a targeted Object type
	 * (denoted by pTargetObjectType)
	 * 
	 * @param pJSONString
	 * @param pTargetObjectType
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Object transformJSONStrinToObject(
		final String pJSONString, final Class pTargetObjectType) {
		
		Object aObj=null;
		Gson aGObj=new Gson();
		
		if (pJSONString!=null && pTargetObjectType!=null) {
			aObj=aGObj.fromJson(pJSONString, pTargetObjectType);
		}
		return aObj;
	}
	
}
